#include <algorithm>
#include "array.h"

Array::Array(size_t cap)
		: size(0),
		  capacity(cap),
		  data(new int[cap]) {}

void Array::swap(Array& array) {
	std::swap(size, array.size);
	std::swap(capacity, array.capacity);
	std::swap(data, array.data);
}

Array::Array(const Array& array) {
	size = array.size;
	capacity = array.capacity;
	data = new int[capacity];
	for (size_t i = 0; i < size; i++) {
		data[i] = array.data[i];
	}
}

Array& Array::operator=(const Array& array) {
	Array tmp(array);
	swap(tmp);
	return *this;
}

Array::~Array() {
	delete[] data;
}

void Array::push_back(int v) {
	if (size >= capacity) {
		capacity = size * 2;
		int* newdata = new int[capacity];
		for (size_t i = 0; i < size; i++) {
			newdata[i] = data[i];
		}
		delete[] data;
		data = newdata;
	}
	data[size++] = v;
}

void Array::set(size_t i, int v) {
	if (i < size) {
		data[i] = v;
	}
}

int Array::get(size_t i) const {
	if (i < size) {
		return data[i];
	}
	return 0;
}

size_t Array::get_size() const {
	return size;
}

